// pages/home/index.js 
let searchKey=''
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNeedSaoMa:app.globalData.isNeedSaoMa,
    banner:[
      {
        id:1,
        picUrl:'/icons/banner1.jpg'
      },
      {
        id:2,
        picUrl:'/icons/banner2.jpg'
      },
      {
        id:3,
        picUrl:'/icons/banner3.jpg'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTopBanner()
    searchKey=''
  },
  goFood(){
    wx.navigateTo({
      url: this.checkFoodPage(),
    })
  },
  diancan(){
    if(app.globalData.isNeedSaoMa){
      wx.scanCode({})
      .then(res=>{
        app.globalData.address=res.result
        wx.navigateTo({
          url: this.checkFoodPage(),
        })
      })
    }
    else{
      wx.navigateTo({
        url:this.checkFoodPage(),
      })
    }
  },
  goToFood(){
    wx.navigateTo({
      url: this.checkFoodPage(),
    })
  },
  goToPhone(){
    wx.makePhoneCall({
      phoneNumber: '15179633397',
    })
  },
  paihao(){
    wx.navigateTo({
      url: '/pages/paihao/paihao',
    })
  },
  getSearch(e){
    searchKey=e.detail.value 
  },
  goSearch(){
    wx.navigateTo({
      url: this.checkFoodPage()+'?searchKey='+searchKey,
    })
  },
  checkFoodPage(){
    if(app.globalData.isNeedSaoMa){
      return '/pages/food2/food2'
    }else{
      return '/pages/food1/food1'
    }
  },
  getTopBanner(){
    wx.cloud.database().collection('lunbotu')
    .get()
    .then(res=>{
      // console.log("请求的数据",res)
      if(res.data && res.data.length>0){
        this.setData({
          banner:res.data
        })
      }
    })
    .catch(res=>{
      console.log(res)
    })
  }

})